﻿using System;
using System.Globalization;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class ListClient : System.Web.UI.Page
    {
        private ClientEntity _client;
        private int _roleId;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
            
            if (Session["RoleId"]!=null )
            {
                _roleId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
                if (_roleId == 1)
                { 
                    if(!IsPostBack)
                    {
                        BindData(); 
                    }
                }
                else if (_roleId == 3)
                {
                    Response.Redirect("ViewClientDetail.aspx");
                }
            }
        }

        private void BindData()
        {
            ListViewClient.DataSource = ClientProfileLogic.SelectAllClient();
            ListViewClient.DataBind();             
        }

        protected void ListViewClientItemCommand(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                var id = int.Parse(e.CommandArgument.ToString(), CultureInfo.InvariantCulture);

                _client = new ClientEntity {ClientId = id};

                ClientProfileLogic.DeleteClient(_client);

                BindData();
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ButtonSearchClick(null, null);
            ListViewClient.DataBind();
        }
        /// <summary>
        /// Filter search clients
        /// </summary>
        protected void FilterClient()
        {
            var fullName = textFullName.Text;
            var companyName = textCompanyName.Text;

            var query = ClientProfileLogic.SearchClient(fullName, companyName);
            if(query==null)
            {
                labelResult.Text = "Not found data";
                return;
            }
            ListViewClient.DataSource = query;
            ListViewClient.DataBind();

        }
        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            FilterClient();
        }

        /* or we get "raised event ItemDeleting which wasn't handled" */
        protected void ListViewClientItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
        }

        protected void LinkDelSelectedClick(object sender, EventArgs e)
        {
            foreach (var listViewDataItem in ListViewClient.Items)
            {
                var checkBox = listViewDataItem.FindControl("checkDelete") as HtmlInputCheckBox;

                if(checkBox!=null && checkBox.Checked)
                {
                    var a = checkBox.Value;
                    var clientEntity = new ClientEntity
                                           {
                                               ClientId = int.Parse((a), CultureInfo.InvariantCulture)
                                           };
                    var result = ClientProfileLogic.DeleteClient(clientEntity);
                    if (result != "Successfully!")
                    {
                        return;
                    }
                } 
            }
            BindData();
        }

        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.ListClient;

            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }
    }
}